<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.xh')"
            >
              <a-input v-model="queryParam.xh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item
              :label="$t('key.name')"
            >
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.stuCate')"
              >
                <a-select allowClear v-model="queryParam.xslb">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.nj')"
              >
                <a-select :allowClear="true" v-model="queryParam.sznj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.yx')"
              >
                <a-select allowClear v-model="queryParam.ssyxm" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.zy')"
              >
                <a-select :allowClear="true" v-model="queryParam.sszym" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+ '- ' +i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.khzt')"
              >
                <a-select :allowClear="true" v-model="queryParam.khzt">
                  <a-select-option v-for="i in TYPE0339" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item
                :label="$t('key.sftg')"
              >
                <a-select :allowClear="true" v-model="queryParam.sftg">
                  <a-select-option v-for="i in TYPE0117" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>

          <a-col :md="8" :sm="16">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">{{ $t('button.query') }}</a-button>
              <a-button style="margin: 0 10px" @click="() => queryParam = {}">{{ $t('button.reset') }}</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? $t('button.packUp') : $t('button.advance') }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>
    
    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        ref="table"
        size="default"
        rowKey="xh"
        :columns="columns"
        :data="loadData"
        :alert="{show: false}"
        :pagination="{ pageSize: 10 }"
        :scroll="{ y: 600, x: 1600 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>

        <template slot="operation" slot-scope="a,b">
          <a @click="showCheck(b.xh)">{{ $t('button.view') }}</a>&nbsp;
          <template v-if="b.zqkhzt == '09' || b.zqkhzt == '13'">
            <a-divider type="vertical"/>
            <a @click="withdraw(b.id)">{{ $t('button.withdraw') }}</a>
          </template>
        </template>
      </s-table>
    </a-card>

    <check-modal
      ref="check"
      :visible="visible"
      :isCheck="false"
      @cancel="visible = false"
    />
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import moment from 'moment';
import checkModal from '../components/checkModal'

export default {
  name: 'midTerm-query',
  data() {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: this.$t('key.no'),
          width: '100px',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: this.$t('key.name'),
          width: '120px',
          dataIndex: 'xm'
        },
        {
          title: this.$t('key.ssyx'),
          width: '150px',
          dataIndex: 'ssyxmc'
        },
        {
          title: this.$t('key.zy'),
          width: '150px',
          dataIndex: 'sszymc'
        },
        {
          title: this.$t('key.nj'),
          dataIndex: 'sznj'
        },
        {
          title: this.$t('key.stuCate'),
          width: '150px',
          dataIndex: 'xslbmc'
        },
        {
          title: this.$t('key.dsxm'),
          width: '120px',
          dataIndex: 'dsxm'
        },
        {
          title: this.$t('key.zt'),
          width: '150px',
          dataIndex: 'zqkhztmc'
        },
        {
          title: this.$t('key.khsftg'),
          width: '120px',
          dataIndex: 'sftgzqkhmc'
        },
        {
          title: this.$t('key.createDate'),
          width: '180px',
          dataIndex: 'createDate'
        },
        {
          title: this.$t('key.option'),
          width: '140px',
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        },
      ],
      loadData: parameter => {
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.dsshQuery(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      visible: false
    }
  },
  components: { STable, checkModal },
  computed: {
    ...mapState({
      TYPE0075: state => state.app['TYPE0075'], // 学生类别
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0339: state => state.app['TYPE0339'], // 考核状态
      TYPE0117: state => state.app['TYPE0117'], // 是否通过
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0339')
    this.$store.dispatch('app/setDictionary', 'TYPE0117')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    showCheck (xh) {
      console.log(xh)
      this.visible = true
      this.$api.cultivate.dsshDetail(xh).then(res => {
        this.$refs.check.detail = res.data
      })
    },
    withdraw (id) {
      this.$api.cultivate.zqkhBack(id).then(res => {
        res.status == 1 ? this.$message.success('退回成功') : this.$message.error(res.msg)
        this.$refs.table.refresh(true)
      })
    }
  }
}
</script>
